package ATM;

import javax.swing.*;
import javax.swing.plaf.metal.MetalIconFactory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransferJpanel extends JPanel implements ActionListener {
    private Icon pane = Util.getimagesIcon("/images/pane.jpg");
    private Icon buttonOff = Util.getimagesIcon("/images/buttonOff.jpg");
    private JLabel jLabelTrans;
    private JLabel transfer;

    private JButton jButtonTrans;
    private JTextField jTextFieldTrans;

    private JLabel jLabelMoney;
    private JTextField jTextFieldMoney;
    private JButton btnOff;
    private ATMJframe atmJframe;

    public TransferJpanel(ATMJframe atmJframe) {
        this.atmJframe = atmJframe;
        this.setLayout(null);
        this.setOpaque(false);
        jLabelTrans = new JLabel("请输入要转账的ID：");
        jLabelTrans.setBounds(300, 200, 250, 50);
        jLabelTrans.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jLabelTrans);

        jTextFieldTrans = new JTextField();
        jTextFieldTrans.setBounds(520, 200, 300, 50);
        jTextFieldTrans.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jTextFieldTrans);


        //
        jLabelMoney = new JLabel("请输入转账金额：");
        jLabelMoney.setBounds(300, 250, 250, 50);
        jLabelMoney.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jLabelMoney);

        jTextFieldMoney = new JTextField();
        jTextFieldMoney.setBounds(520, 250, 300, 50);
        jTextFieldMoney.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jTextFieldMoney);


        transfer = new JLabel("转  账");
        transfer.setBounds(330, 300, 140, 50);
        transfer.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 25));
        transfer.setForeground(Color.WHITE);
        this.add(transfer);

        jButtonTrans = new JButton("转账", pane);
        jButtonTrans.setBounds(300, 300, 140, 50);
        jButtonTrans.setOpaque(false);
        this.add(jButtonTrans);
        jButtonTrans.addActionListener(this);


        btnOff = new JButton("", buttonOff);
        btnOff.setBounds(850, 550, 140, 50);
        this.add(btnOff);
        btnOff.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnOff) {
            atmJframe.switchPanel("menu");
        } else if (e.getSource() == jButtonTrans) {
//             获取输入的对方账号
            String otherId = jTextFieldTrans.getText();
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(jTextFieldMoney.getText());

            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }
            //当前登录的账户
            UtilSql1 utilSql1 = new UtilSql1();
            Users loginUser = utilSql1.getById(atmJframe.getUser().getId());
            Users otherUser = utilSql1.getById(otherId);
            // 判断账号是否存在
            if (loginUser.getId().equals(otherId)) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                return;
            }
            // 判断余额是否足够
            if (blance > loginUser.getCount()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
            // 当期账户扣钱

            loginUser.setCount(loginUser.getCount() - blance);
            // 对方账户加钱

            otherUser.setCount(loginUser.getCount() + blance);
            utilSql1.transition(loginUser,otherUser);
//            utilSql1.update(loginUser);
//            utilSql1.update(otherUser);
            JOptionPane.showMessageDialog(this,"转账成功");
            atmJframe.switchPanel("menu");




        }
//        if (e.getSource() == btnOff) {
//            atmJframe.switchPanel("menu");
//        } else if (e.getSource() == jButtonTrans) {
//            double money = 0;
//            try {
//                 money = Double.parseDouble(jTextFieldMoney.getText());
//
//            } catch (NumberFormatException exception) {
//                JOptionPane.showMessageDialog(this, "金额只能为数字且不能为空");
//                return;
//            }


//            for (int i = 0; i < atmJframe.getUsersArrayList().size(); i++) {
//                if (jTextFieldTrans.getText().equals(atmJframe.getUser().getId())) {
//                    JOptionPane.showMessageDialog(this, "不能转账给自己");
//                    return;
//                } else if (jTextFieldTrans.getText().equals(atmJframe.getUsersArrayList().get(i).getId()) && !jTextFieldTrans.getText().equals(atmJframe.getUser().getId())) {
//                    if (money > atmJframe.getUser().getCount()) {
//                        JOptionPane.showMessageDialog(this, "余额不足，转账失败");
//                    } else {
//                        JOptionPane.showMessageDialog(this,"转账成功");
//
//                        //修改数据库
////                        atmJframe.getTest01().transfer(atmJframe.getUser().getId(),atmJframe.getUser().getCount(),atmJframe.getUsersArrayList().get(i).getId(),atmJframe.getUsersArrayList().get(i).getCount(),money);
//
//                        //修改User
//                        atmJframe.getUser().setCount(atmJframe.getUser().getCount() - money);
//
//                        atmJframe.getUsersArrayList().get(i).setCount(atmJframe.getUsersArrayList().get(i).getCount() + money);
//
////                        FileUtil.writeObject("a.dat",atmJframe.getUsersArrayList());
//
//                    }
//                    return;
//                }
//
//            }
//            JOptionPane.showMessageDialog(this, "查无此人");


    }
}

